import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wan_android_flutter/pages/knowledge/detail/knowledge_detail_vm.dart';
import 'package:wan_android_flutter/pages/knowledge/detail/knowledge_tab_child_page.dart';

import '../../../repository/datas/knowledge_list_data.dart';

class knowledgeDetailTabPage extends StatefulWidget {
  const knowledgeDetailTabPage(this.tabList, {super.key});

  final List<KnowledgeChildren>? tabList;

  @override
  State createState() {
    return _knowledgeDetailTabPageState();
  }
}

class _knowledgeDetailTabPageState extends State<knowledgeDetailTabPage>
    with SingleTickerProviderStateMixin {
  knowledgeDetailViewModel viewModel = knowledgeDetailViewModel();
  late TabController tabController;

  @override
  void initState() {
    super.initState();
    tabController =
        TabController(length: widget.tabList?.length ?? 0, vsync: this);
    viewModel.initTabs(widget.tabList);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) {
        return viewModel;
      },
      child: Scaffold(
        appBar: AppBar(
          title: TabBar(
            tabs: viewModel.tabs,
            controller: tabController,
            labelColor: Colors.blue,
            indicatorColor: Colors.blue,
            isScrollable: true,
          ),
        ),
        body: SafeArea(
          child: TabBarView(
            controller: tabController,
            children: children(),
          ),
        ),
      ),
    );
  }

  List<Widget> children() {
    return widget.tabList?.map((e) {
          return knowledgeTabChildPage(
            cid: "${e.id ?? ""}",
          );
        }).toList() ??
        [];
  }
}
